package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service

public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 新增分类
     * @param dto
     */
    @Override
    public void add(CategoryDTO dto) {

        log.info("新增分类：{}",dto);
        Category category = new Category();

        //属性拷贝
        BeanUtils.copyProperties(dto,category);

        //1.补充缺少的属性值
        //使用公共字段自动填充逻辑
//        category.setStatus(StatusConstant.ENABLE);
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());

        //2.调用mapper方法，将数据保存到数据库中
        categoryMapper.insert(category);


        log.info("新增成功");
    }

    /**
     * 分类分页查询
     * @param queryDTO
     * @return
     */
    @Override
    public PageResult page(CategoryPageQueryDTO queryDTO) {

        //1.设置分页参数
        PageHelper.startPage(queryDTO.getPage(),queryDTO.getPageSize());

        //2.调用mapper分页查询，强转返回类型为Page
        Page<Category> page =  categoryMapper.list(queryDTO);

        //3.封装PageResult并返回
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 启用、禁用分类
     */
    @Override
    public void enableOrDisable(Integer status, Long id) {

        Category category= Category.builder()
                .id( id)
                .status(status)
                .build();

        categoryMapper.update(category);

    }

    /**
     * 删除分类
     */
    @Override
    public void delete(Long id) {
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Integer count = dishMapper.countByCategoryId(id);
        if(count>0)
        {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        count = setmealMapper.countByCategoryId(id);
        if(count>0)
        {
            //当前分类下有套餐，不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除分类
        categoryMapper.delete(id);

    }

    /**
     * 修改分类
     */
    @Override
    public void update(CategoryDTO dto) {
        Category category = new Category();
        // 拷贝
        BeanUtils.copyProperties(dto,category);

        //使用公共字段自动填充逻辑
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.update(category);
    }

    /**
     * 查询分类
     */
    @Override
    public List<Category> list(Integer type) {

        return categoryMapper.list2(type);
    }
}
